<template>
  <div class="login">
    <Form class="form" ref="form" :model="form" :rules="rule" @keyup.enter.native="handleSubmit('form')">
      <FormItem prop="user">
        <Input type="text" v-model="form.user" placeholder="用户名">
        <Icon type="ios-person-outline" slot="prepend" />
        </Input>
      </FormItem>
      <FormItem prop="password">
        <Input type="password" v-model="form.password" placeholder="密码">
        <Icon type="ios-lock-outline" slot="prepend" />
        </Input>
      </FormItem>
      <FormItem>
        <Button type="primary" @click="handleSubmit('form')">登录</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  layout: 'blank',
  data () {
    return {
      form: {
        user: '',
        password: ''
      },
      rule: {
        user: [
          { required: true, message: '请填写用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { type: 'string', min: 6, message: '密码至少6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$router.push('/home')
        } else {
          this.$Message.error('表单校验失败')
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
  .login {
    padding-top: 20vh;
    .form {
      width: 400px;
      margin: 0 auto;
    }
  }
</style>
